package com.xt.purchasingsystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xt.purchasingsystem.config.JwtProperties;
import com.xt.purchasingsystem.feign.SysUsersClient;
import com.xt.purchasingsystem.model.SysUsers;
import com.xt.purchasingsystem.service.AuthorService;
import com.xt.purchasingsystem.utils.ConvertUtil;
import com.xt.purchasingsystem.utils.JwtUtils;
import com.xt.purchasingsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 老姚
 * Date:  2020/5/22 18:22
 */
@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthorServiceImpl implements AuthorService {
    @Autowired
    SysUsersClient sysUsersClient;
    @Autowired
    private JwtProperties properties;

    @Override
    public Map<String,Object> accredit(SysUsers sysUsers) {
        Result result = sysUsersClient.login(sysUsers);
        if (result.getData()==null){
            return null;
        }
        SysUsers user = ConvertUtil.convertValue(result.getData(), SysUsers.class);
        if (user == null) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("id", user.getId());
        map.put("photoPath", user.getPhotoPath());
        map.put("loginId", user.getLoginId());
        try {
            String token = JwtUtils.generateToken(map, this.properties.getPrivateKey(), this.properties.getExpire());
           Map<String,Object> hashMap=new HashMap<>();
           hashMap.put("token",token);
           hashMap.put("user",user);
            return hashMap;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
